


pub mod model;
mod service;
pub mod controller;
pub mod authentication;


use axum::Router;
use axum::routing::{get, post};

/* router.nest("/user", config_router()) */
pub fn config_router() -> Router
{
    Router::new()
        .route("/:id", get(controller::get_user_by_id))
        .route("/register", post(controller::register))
        .route("/login", post(controller::login))
}


#[cfg(test)]
mod tests
{
    use super::*;
    use crate::test;


    async fn app() -> axum::Router {
        test::init_app_test_one(Option::None, "/user", config_router()).await
    }


    #[tokio::test]
    async fn test_get_user_by_id()
    {
        let response = test::get(app().await, "/user/1").await;
        println!("{}", response.text());
    }


    #[tokio::test]
    async fn test_register()
    {
        let user = model::User::new(1, "weifei", "weifei", Option::None);
        let response = test::post_json(app().await, "/user/register", &user).await;
        println!("{}", response.text());
    }

    #[tokio::test]
    async fn test_login_function()
    {
        crate::db::init_rbatis("mariadb://root:weifei@localhost:3306/blog?useSSL=false");
        let user = model::User::new(0, "weifei", "weifei", Option::None);
        // let result = controller::login(axum::Json(user)).await;
        // println!("{:#?}", result);
    }

    #[tokio::test]
    async fn test_login_success()
    {
        let user = model::User::new(0, "weifei", "weifei", Option::None);
        let response = test::post_json(app().await, "/user/login", &user).await;
        println!("{}", response.text());
        assert_eq!(response.text(), rust_i18n::t!("user.login.success"));
    }

    #[tokio::test]
    async fn test_login_not_exists()
    {
        let user = model::User::new(0, "root", "weifei", Option::None);
        let response = test::post_json(app().await, "/user/login", &user).await;
        println!("{}", response.text());
        assert_eq!(response.text(), rust_i18n::t!("user.login.failed.not_exists"));
    }

    #[tokio::test]
    async fn test_login_password_wrong()
    {
        let user = model::User::new(0, "weifei", "123456", Option::None);
        let response = test::post_json(app().await, "/user/login", &user).await;
        println!("{}", response.text());
        assert_eq!(response.text(), rust_i18n::t!("user.login.failed.password_wrong"));
    }

}


